package com.gzhryc.shared_device.common.payment.services;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.base.enums.ETransferState;
import com.gzhryc.shared_device.common.payment.dao.WXTransferMoneyOrderDao;
import com.gzhryc.shared_device.common.payment.dao.db.WXTransferMoneyOrder;

import java.sql.SQLException;

public class WXTransferMoneyOrderService {

    static Logger log = Logger.getLogger(WXTransferMoneyOrderService.class);

    public static WXTransferMoneyOrderService self() {
        return MultiDBTools.getService(WXTransferMoneyOrderService.class, true);
    }

    public static WXTransferMoneyOrderService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, WXTransferMoneyOrderService.class, true);
    }

    WXTransferMoneyOrderDao dao;

    public WXTransferMoneyOrderService(String jdbcKey) {
        dao = new WXTransferMoneyOrderDao(jdbcKey);
    }

    public WXTransferMoneyOrder getByTradeNo(String tradeNo){
        try {
            return dao.getById(tradeNo);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public boolean add(WXTransferMoneyOrder transferMoneyOrder){
        try {
            return  dao.insert(transferMoneyOrder) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }

    public boolean updateExternalInfo(String tradeNo,String externalTradeNo,String externalContent,String transferAccount){
        if(StringTools.isNotBlank(tradeNo) && StringTools.isNotBlank(externalContent)){
            WXTransferMoneyOrder updateEntity = new WXTransferMoneyOrder();
            updateEntity.setTradeNo(tradeNo);
            updateEntity.setExternalTradeNo(externalTradeNo);
            updateEntity.setExternalContent(externalContent);
            updateEntity.setTransferAccount(transferAccount);
            updateEntity.setTransferStatus(ETransferState.Processing.index());

            try {
                return  dao.updateNotNull(updateEntity) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return false;
    }

    public boolean toTransferMoneyFail(String tradeNo,String failNote){
        if(StringTools.isNotBlank(tradeNo) && StringTools.isNotBlank(failNote)) {
            Conditions conditions = new Conditions(WXTransferMoneyOrder.class);
            conditions.and().is("tradeNo", tradeNo);
            conditions.and().is("transferStatus", ETransferState.Processing.index());

            WXTransferMoneyOrder updateEntity = new WXTransferMoneyOrder();
            updateEntity.setTransferStatus(ETransferState.Fail.index());
            updateEntity.setFailNote(failNote);

            try {
                return dao.updateNotNull(updateEntity) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return false;
    }

    public boolean toClose(String tradeNo,String failNote){
        if(StringTools.isNotBlank(tradeNo) && StringTools.isNotBlank(failNote)) {
            Conditions conditions = new Conditions(WXTransferMoneyOrder.class);
            conditions.and().is("tradeNo", tradeNo);
            conditions.and().is("transferStatus", ETransferState.Processing.index());

            WXTransferMoneyOrder updateEntity = new WXTransferMoneyOrder();
            updateEntity.setTransferStatus(ETransferState.Close.index());
            updateEntity.setFailNote(failNote);

            try {
                return dao.updateNotNull(updateEntity) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return false;
    }
    
    public boolean deleteByTradeNo(String tradeNo) {
    	if(StringTools.isNotBlank(tradeNo)) {
    		try {
				return dao.deleteById(tradeNo) > 0;
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
    	}
    	return false;
    }
}
